<template>
	<view>
		<view @touchmove.prevent.stop style="z-index: 999;">
			<u-popup  v-model="loginshow" mode="center" ref="auth" width="85%" :mask-close-able="false" :custom="true" :mask-click="false" @change="maskClick">
				<view class="uni-tip">
					<image class="tipIv" :src="ossThree('/uploads/20250404/573dd121b664a0b0565b364c65a10f31.png')" mode=""></image>
					<view class="tipTv">牛牛日结</view>
					<view class="loginTv1" @click="onLoginClick()" v-if="newlei==2">直接登录</view>
					<button v-if="checked&&newlei==1" type="default" class="loginTv1" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">绑定手机号</button>
					<button v-if="!checked&&newlei==1" type="default" class="loginTv1" @click="gethuoqu">绑定手机号</button>
					<button v-if="checked&&newlei==3" type="default" class="loginTv1" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumbernew">绑定手机号</button>
					<button v-if="!checked&&newlei==3" type="default" class="loginTv1" @click="gethuoqu">绑定手机号</button>
					<view class="xuzhiV">
						<u-checkbox 
							@change="checkboxChange" 
							v-model="checked"
							active-color='#06C15F'
							icon-size='10'
							label-size='24'
							size='26'
						>
						<view class="xuzhiTv1">
							我已阅读并同意
							<text style="color: #06C15F;" @click.stop="navrouter('/pages/my/yhXieyi?key=yonghuxieyi')">《用户协议》</text>
							<!-- 和
							<text style="color: #06C15F;" @click.stop="navrouter('/pages/my/yhXieyi?key=yinsizhengce')">《隐私协议》</text> -->
						</view>
						</u-checkbox>
					</view>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'ns-login',
		props: {
			loginshow: {
				type: Boolean,
				default: false
			},
			session_key:{
				type: String,
				default: ''
			},
			lei:{
				type: String,
				default: 1,//2就是详情进入 需要登陆
			},
			invite_code:{
				type: String,
				default: '',//2就是详情进入 分享携带
			}

		},
		data() {
			return {
				isShow: true,
				xieyi: false,
				checked:true,
				shuchu:'',
				detail:{
					code:'',
					encryptedData:'',
					errMsg:'',
					iv:''
				},
				token:'',
				newlei:1,
				newsession_key:''
			};
		},
		created() {
			this.newlei = this.lei
		},
		mounted() {

		},
		methods: {
			checkboxChange(e) {
				console.log(e);
				this.checked = e.value
				console.log(this.checked)
			},
			gethuoqu(){
				if (!this.checked) {
					this.$u.toast("请先阅读并同意隐私协议")
					return
				}
			},
			getxieyi(){
				this.navrouter("/pages/my/yhXieyi?key=agreement");
			},
			decryptPhoneNumber(e){ 
				console.log(e);
				
				if (!this.checked) {
					this.$u.toast("请先阅读并同意隐私协议")
					return
				}
				if(e.detail.errMsg == 'getPhoneNumber:ok'){
					this.shuchu = JSON.stringify(e)
					this.detail = e.detail
					this.getphonenew();
				}else{
					this.$u.toast("请先授权获取手机号")
				}
				
			},
			async getphonenew(){
				let res = await this.$u.api.auth_login({
					code:this.detail.code,
					session_key:this.session_key,
					iv:this.detail.iv,
					encryptedData:this.detail.encryptedData
					// userid:uni.getStorageSync('userid')
				});
				console.log(res)
				//登录成功
				this.$emit("saveUserInfo", {
					code: 200
				})
			},
			onChooseAvatar(e) {
				console.log(e)
				let {avatarUrl} = e.detail
				// 图片直接上传到服务器
				// upload(...)
			},
			
			// 用户昵称改变
			nicknameChange(val){
				console.log(val)
				// this.nickName = val
			},
			open(data) {
				if (data) {
					this.isShow = data.show;
				}
				this.$refs.auth.open();
			},
			close() {
				this.$refs.auth.close();
			},
			maskClick(e) {
				let isShow = e.show;
				if (!isShow) {
					this.$emit("saveUserInfo", {
						code: 202
					})
				}
			},
			onNoLoginClick() {
				this.$refs.auth.close();
			},
			onLoginClick() {
				if (!this.checked) {
					this.$u.toast("请先阅读并同意隐私协议")
					return
				}
				uni.login({
					provider: 'weixin',
					timeout: 3000,
					success: res => {
						if (res.code) {
							this.getLogintwo(res.code)
						}
					},
					fail: () => {
						this.$u.toast("请求失败")
					}
				})

			},
			async getLogintwo(code){
				let res = await this.$u.api.miniapplogin({
					code:code,
					identity:this.$store.state.identity||uni.getStorageSync('identity'),//2老板
					invite_code:this.invite_code
				});
				console.log(res.userinfo)
				uni.setStorageSync('token',res.userinfo.token)
				this.$store.commit('user/changeToken', res.userinfo.token);
				if(res.userinfo.mobile==''||!res.userinfo.mobile){
					//绑定手机号
					// console.log(res.session_key)
					this.$u.toast("登录成功,请先授权获取手机号")
					this.newsession_key = res.session_key
					this.lei = 1;//绑定手机号
					this.token = res.userinfo.token
					// uni.redirectTo({ url: '/pages/login/loginPhone?session_key='+res.session_key })
				}else{
					
					this.$u.toast('登录成功');
					this.$emit("saveUserInfo", {
							code: 202
						})
				}
			},
			decryptPhoneNumbernew(e){ 
				console.log(e);
				
				if (!this.checked) {
					this.$u.toast("请先阅读并同意隐私协议")
					return
				}
				if(e.detail.errMsg == 'getPhoneNumber:ok'){
					this.shuchu = JSON.stringify(e)
					this.detail = e.detail
					this.getphone();
				}else{
					this.$u.toast("请先授权获取手机号")
				}
				
			},
			async getphone(){
				let res = await this.$u.api.auth_login({
					code:this.detail.code,
					session_key:this.newsession_key,
					iv:this.detail.iv,
					encryptedData:this.detail.encryptedData
					// userid:uni.getStorageSync('userid')
				});
				console.log(res)
				//登录成功
				this.$emit("saveUserInfo", {
					code: 200
				})
			},
			async login(code, encryptedData, iv) {
				let res = await this.$u.api.login({
					wxcode: code,
					iv: iv,
					encryptedData: encryptedData,
				});
				console.log(res)
				// this.$api.login({
				// 	code: code,
				// 	iv: iv,
				// 	encryptedData: encryptedData,
				// }).then((res) => {
				// 	uni.$u.toast("登录成功")
				// 	uni.setStorageSync("user_info_token", res.userinfo.token);
				// 	uni.setStorageSync("user_info_id", res.userinfo.id);
				// 	uni.setStorageSync("user_info", res.userinfo);
				// 	this.$emit("saveUserInfo", {
				// 		code: 200
				// 	})
				// 	this.$refs.auth.close();
				// }).catch((e) => {
				// 	uni.$u.toast(e.msg === undefined ? "登录失败" : e.msg)
				// })
			},
			onAgreementClick() {
				this.$u.route({
					url: '/mine/me/Agreement',
					params: {
						type: 3,
					}
				})
			},

		}
	};
</script>

<style lang="scss">
	.uni-tip {
		width: 100%;
		background: #fff;
		box-sizing: border-box;
		border-radius: 0rpx;
		overflow: hidden;
		height: initial;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 30rpx;
		z-index: 9999;
		border-radius: 20rpx;
	}

	.tipIv {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
	}

	.tipTv {
		font-size: 30rpx;
		color: #333;
		margin-top: 30rpx;
	}

	.loginTv1 {
		width: 80%;
		margin-top: 70rpx;
		height: 70rpx;
		border-radius: 35rpx;
		background: #06C15F !important;
		border: none;
		// background: linear-gradient(0deg, rgb(252, 141, 0), rgb(254, 83, 1));
		color: #fff !important;
		font-size: 26rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.loginTv2 {
		width: 80%;
		margin-top: 30rpx;
		height: 70rpx;
		border-radius: 35rpx;
		background: rgba(0, 0, 0, 0.4);
		// background: linear-gradient(0deg, rgb(252, 141, 0), rgb(254, 83, 1));
		// border: 1rpx solid #D4FF45;
		color: #fff;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.xuzhiV {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 35rpx;

		.xuzhiIv {
			width: 30rpx;
			height: 30rpx;
		}

		.xuzhiTv1 {
			margin-left: 8rpx;
			font-size: 28rpx;
			color: #8D8D8D;
		}

		.xuzhiTv2 {
			font-size: 28rpx;
			color: #8D8D8D;
		}
	}
</style>
<style scoped>

</style>
